home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Calculators / NumberCrunch 1.41 / Number Crunch II Demo / Library Routines / Text Examples / Random Numbers and Noise < prev    next >
Encoding:
Text File  |  1992-09-03  |  7.3 KB  |  258 lines  |  [TEXT/NCII]

  1. ##############
  2. # Random Numbers and Noise
  3. #  program InitRandom(seed)  # Intitializes all random routines; seed ~ 1.0 . 
  4. #   function Random # Returns a uniform random number between 0 and 1.
  5. #   function RandomArray(N) # Returns an array of N randoms.
  6. #   function RandomInRange(J,L) # Returns a random integer K, J<=K<=L
  7. #   function WhiteNoise(n) # Returns n-array of uniform, sigma=1 white noise.
  8. #   function GaussianNoise(N) # Returns array of gaussian noise, sigma=1.
  9. #  program MakeDistribution(Y,max,N,  x,P) # Bin randoms y into a distribution P
  10. #  This text file explains and gives examples of
  11. #  some of the routines in the file All Library Routines, which
  12. #  should be Opened before trying any of these examples.
  13. ##################
  14.  
  15. ##############
  16. # The xCOD itself is
  17. xRANDOM
  18.   # xCO2 xRANDOM(zInit,N,x:extended; RandStore:RealArray) , sets  0<x[1…N]<1 to random numbers. RandStore[1..100] must be preserved between calls. zInit <0 is a signal to initialize RandStore with zInit seed ; an external program.
  19.  
  20. ############
  21. # The interface routines.
  22. # Examples are given after each.
  23.  
  24. # This must be called once, initially, with a 0-10 or so
  25. # random seed.  The storage array RandStore must
  26. # exist between successive calls to xRANDOM.
  27.  program InitRandom(seed)  # Intitializes all random routines; seed ~ 1.0 . 
  28. .   var x
  29. .   #  Input:  seed = real random number ~ 1.0
  30. .   #  Output:  none.  Sets up RandStore.
  31. .   begin
  32. .     RandStore[1…100] = 0
  33. .     x[1…2] = 0
  34. .     xRANDOM(-314159*seed, size(x),x,RandStore)
  35. .   end
  36. .   
  37.  
  38. # If you want a different set of 'randoms', use a different
  39. # seed than the one here.
  40. InitRandom(0.133241)
  41.  
  42. ###########################
  43. # Here a number of different uses of xRandom.
  44. # Examples are given for each.
  45.  
  46. # This returns a single random number
  47. #  uniformly distributed from 0 to 1.
  48. #
  49.   function Random # Returns a uniform random number between 0 and 1.
  50. .   var x
  51. . #  Input: none
  52. . #  Ouput:   0 < Random < 1
  53. .   begin
  54. .     x=0
  55. .     xRandom(1,1,x,RandStore)
  56. .     Random=x
  57. .   end
  58. .   
  59.  
  60. # For example, 
  61. Random               # type this, hit return
  62.   0.70898038     # and get this the 1st time
  63. Random
  64.   0.57992348     # but this the second.
  65.  
  66.  
  67. # Returns an array of N random numbers between 0 and 1.
  68. # Much faster than successive calls to Random, above.
  69. #
  70.   function RandomArray(N) # Returns an array of N randoms.
  71. .   var x
  72. .  #   Input:    N = positive integer
  73. .  #   Output:  RandomArray = x[1…N] random numbers, all 0 -> 1.
  74. .   begin
  75. .     x[1…n]=0
  76. .     xRandom(1,n,x,RandStore)
  77. .     RandomArray = x
  78. .   end
  79. .   
  80.  
  81. # For example,
  82. RandomArray(5)
  83.   [0.85127, 0.2307, 0.65812, 0.64353, 0.11952] 
  84.  
  85.  
  86. # Gives a random integer K in the range  J <= K <= L.  
  87. #  Note that the Int(x) function rounds to
  88. #  the nearest even integer.
  89. #
  90.   function RandomInRange(J,L) # Returns a random integer K, J<=K<=L
  91. .   var x, a, b
  92. . #   Input:   J, L = integers
  93. . #   Ouput:   RandomInRange = random integer from J to L.
  94. .   begin
  95. .     a = J
  96. .     b = L
  97. .     a=a-.5+1e-17
  98. .     b=b+.5-1e-17
  99. .     x=0
  100. .     xRandom(1,1,x,RandStore)
  101. .     x=a+x*(b-a)
  102. .     RandomInRange=Int(x)
  103. .   end
  104. .   
  105.  
  106.  
  107. # For example, to simulate the roll of a die,
  108. RandomInRange(1,6)
  109.   4 
  110. RandomInRange(1,6)
  111.   4 
  112. RandomInRange(1,6)
  113.   2 
  114.  
  115. # Here are several more trials:
  116. for j=1:20 do d[j]=RandomInRange(1,6)
  117.   d[1…20] = [3, 5, 1, 2, 5, 3, 5, 4, 5, 6, 4, 4, 6, 6, 2, 3, 4, 2, 6, 5] 
  118.  
  119.  
  120. # Here are two that calculate some statistics.
  121. # These are also in the file Miscellaneous Routines.
  122.   Mean(V) = sum(V)/size(V) # Returns mean of an array V.;
  123.   StandardDeviation(V) = sqrt( Mean(V^2) - Mean(V)^2);
  124.  
  125. # For example, 
  126. Mean(d)   # where d is the dice rolls done above
  127.   4.05 
  128. StandardDeviation(d)
  129.   1.49916644 
  130.  
  131. # The average of a number of dice rolls should be (1+6)/2 = 3.5.
  132. # In N trials, the error in the average should be about 1/sqrt(N).
  133. # For d, with 20 trials, the expected error is then
  134. 1/sqrt(20)
  135.   0.2236068 
  136. # while the actual error actually was
  137. 4.05 - 3.5
  138.   0.55 
  139. # which means that usually we'll get a bit closer than 4.05.
  140.  
  141. # Another trial gave me
  142. for j=1:20 do d[j]=RandomInRange(1,6)
  143.   d[1…20] = [2, 6, 6, 5, 4, 1, 6, 5, 1, 5, 4, 3, 1, 1, 5, 4, 3, 4, 6, 5] 
  144. Average(d)
  145.   3.85 
  146. # which is closer.
  147.  
  148.  
  149. # The average of Random itself is clearly 0.5, halfway from 0 to 1.  Since the average of x^2 is integral(x^2 from 0 to1) = 1/3, 
  150. # the StandardDeviation is
  151. SigmaRand = sqrt(1/3-1/4)
  152.   SigmaRand = 0.28867513 
  153. AverageRand = 0.5
  154.  
  155. # With this we can define a white-noise routine, which
  156. # generates an N component array of  0 mean, 
  157. # standard dev.=1 numbers.
  158. #
  159.   function WhiteNoise(n) # Returns n-array of uniform, sigma=1 white noise.
  160. .   var AverageRand, SigmaRand
  161. .   # Input: N = positive integer
  162. .   # Output: WhiteNoise = array W[1…N] of 0 mean, variance = 1,
  163. .   #                                 uniform random numbers.
  164. .   begin
  165. .     AverageRand = 0.5;  SigmaRand = sqrt(1/3-1/4);
  166. .     WhiteNoise = (RandomArray(n)-AverageRand)/SigmaRand;
  167. .   end
  168. .   
  169.  
  170. # Some white noise.
  171. w = whiteNoise(1000);
  172.  
  173. # And its statistics.
  174. Mean(w)
  175.   -0.04581557           # This should be about zero.
  176. StandardDeviation(w)
  177.   0.97346018             # This should be about 1.
  178. 1/sqrt(1000)
  179.   0.03162278             # within about this tolerance.
  180.  
  181. # Plot [1…100, WhiteNoise(100)] to see a graph of this.
  182. # Each time you hit the plot button it should change.
  183.  
  184.  
  185. # Finally, noise with a gaussian spectrum. 
  186. # For an explanation of this, see a text like
  187. # Numerical Recipes by W. Press et al.
  188. # This returns an N component array of 0 mean
  189. # standard deviation=1 Gaussian (bell-curve)
  190. # distributed noise. 
  191. #
  192.   function GaussianNoise(N) # Returns array of gaussian noise, sigma=1.
  193. .   var x,y
  194. . #  Input:   N = positive integer
  195. . #  Output:   GaussianNoise = G[1…n] = array of 0 mean, sigma=1 randoms.
  196. .   begin
  197. .     x = RandomArray(n)
  198. .     y = RandomArray(n)
  199. .     GaussianNoise = sqrt(-2 ln(x)) sin(2πy)
  200. .   end
  201. .   
  202.  
  203. g = GaussianNoise(300);
  204.  
  205. # To see a scatter plot of this, just plot yArray=g in Quick Setup 
  206. # in the graph.
  207.  
  208. # Seeing the actual probability distrubution is trickier, since you
  209. # want to essentially how many points of g lie in each dx range.
  210. # This counting can be done by using the g itself as an index into
  211. # another array.
  212.  
  213. # This calculates the probability distribution P of Y,
  214. # calculated by binning it into N+1 boxes
  215. # from -max to max. x is also defined, so that plotting [x,P] 
  216. # shows the distribution
  217. #
  218.  program MakeDistribution(Y,max,N,  x,P) # Bin randoms y into a distribution P.
  219. .   var min,max, yScal,j, Ny
  220. .  #  Input:  Y[1…j] = array of random numbers
  221. .  #             max = range of Y to examine, from y=-max to max
  222. .  #             N = number of bins to count Y in.
  223. .  #  Output:
  224. .  #             P[1…(n+1)] distribution
  225. .  #             x[1…(n+1)] points where P is defined.
  226. .   begin
  227. .     P[-N/2…N/2] = 0;   
  228. .     yScal=y*(N/2)/max
  229. .     x=-N/2…N/2;  x =x*(max/(N/2))
  230. .     Ny=size(Y)
  231. .     j=1
  232. .     while j<Ny do
  233. .       begin
  234. .         P[yScal[j]] = P[yScal[j]]+1
  235. .         j=j+1
  236. .       end
  237. .     P = P/sum(P)
  238. .   end
  239. .   
  240.  
  241.  
  242. # In the case of the gausian "g" above, it ranges from
  243. minimum(g)
  244.   -2.58766783 
  245. Maximum(g)
  246.   3.3347715 
  247. # so if we bin it in, say, 30 bins (want some of 100 in each),
  248. # then [x,P] are set by:  (This is slow, give it a few minutes.)
  249. MakeDistribution(g,3.4,30, x,P)
  250.  
  251. # Plotting [x,P] now shows (roughly) a gaussian bell curve. Increase
  252. # the array sizes for a nicer picture.
  253.  
  254.  
  255.